import {Store, Getter, Mutation, Action} from 'decorators/vuex'
import { contains } from '@/utils/commonMethod/method.js'

@Store
export class PhotoStore {
  filters = []

  subFilters = []

  // 存model集合
  selectFilter = {}

  // 存value集合
  selectValue = {}

  currentSuperId = ''

  currentSelects = []

  @Getter
  selectFitler(state) {
    return state.selectFilter
  }

  @Getter
  selectValue (state) {
    return state.selectValue
  }

  @Getter
  currentSelects(state) {
    return state.currentSelects
  }

  @Getter
  currentSuperId(state) {
    return state.currentSuperId
  }

  @Getter
  filters(state) {
    return state.filters
  }

  @Getter
  subFilters(state) {
    return state.subFilters
  }

  @Mutation
  setCurrentSelects (state, selectItems) {
    state.currentSelects = selectItems
  }

  @Mutation
  setSelectFilter (state, selectFilter) {
    state.selectFilter = selectFilter
  }

  @Mutation
  setSelectValue (state, selectValue) {
    state.selectValue = selectValue
  }

  @Mutation
  setCurrentSuperId (state, superId) {
    // console.log('999999')

    // console.log(state.selectFitler)
    // console.log(superId)

    let arr = state.selectFilter[superId]
    if (!arr) {
      state.selectFilter[superId] = []
      state.selectValue[superId] = []
    }

    state.currentSuperId = superId
  }

  @Mutation
  setSubFilters (state, subFilters) {
    state.subFilters = subFilters
  }

  @Mutation
  setFilters (state, filters) {
    state.filters = filters
  }

  @Action clearSelect ({commit}) {
    commit('setSelectFilter', [])
    commit('setCurrentSelects', [])
    commit('setCurrentSuperId', '')
    commit('setSelectValue', {})
  }

  @Action
  changeCurrentSuperId ({commit}, superId) {
    commit('setCurrentSuperId', superId)
  }

  @Action
  async getFiltersByUrl ({state}, url) {
    try {
      const response = await this.$axios.post('/api/' + url)
      if (response) {
        state.filters = response
      }
    } catch (e) {

    }
  }

  @Action
  getSubFilters({state}, superId) {
    for (var i = state.filters.length - 1; i >= 0; i--) {
      const obj = state.filters[i]
      if (obj.id === superId) {
        state.subFilters = obj.child
        return
      }
    }
  }

  @Action
  addFilter ({state}, item) {
    state.selectFitler.push(item)
  }

  @Action selectFilterItem ({state}, item) {
    // 通过superId 找到对应的已选中数组
    const superIdSelectArr = state.selectFilter[state.currentSuperId]
    const superIdSelectValue = state.selectValue[state.currentSuperId]

    // 判断选中的是否已存在
    if (contains(superIdSelectArr, item)) {
      for (let i in superIdSelectArr) {
        if (superIdSelectArr[i] === item) {
          superIdSelectArr.splice(i, 1)
          superIdSelectValue.splice(i, 1)
          state.selectFilter[state.currentSuperId] = superIdSelectArr
          state.selectValue[state.currentSuperId] = superIdSelectValue
          return
        }
      }
    } else {
      superIdSelectArr.push(item)
      if (item && item.value) {
        superIdSelectValue.push(item.value)
      }
      state.selectValue[state.currentSuperId] = superIdSelectValue
      state.selectFilter[state.currentSuperId] = superIdSelectArr
    }
  }

  @Action
  selectAll ({state}, superId) {
    for (var i = state.filters.length - 1; i >= 0; i--) {
      const obj = state.filters[i]
      if (obj.id === superId) {
        const child = state.obj.child

        state.subFilters = obj.child
        return
      }
    }
  }

  @Action getSelectBySuperId({commit, state}, superId) {
    let arr = state.selectFilter[superId]
    commit('setCurrentSelects', arr)
  }

// 取出当前所有选中的条件
// let checkSelect = function findSelect (allselectArr, items, currentSelects) {
//   for (let item of items) {
//     if (allselectArr.contains(item)) {
//       currentSelects.push(item)
//     } else {
//       if (item.children && item.children.length > 0) {
//         checkSelect(allselectArr, item.children)
//       }
//     }
//   }
// }

// Array.prototype.contains = function (needle) {
//   for (i in this) {
//     if (this[i] == needle) return true
//   }
//   return false
// }

// Array.prototype.remove = function (needle) {
  // for (i in this) {
  //   if (this[i] == needle) {
  //     this.splice(i, 1)
  //   }
  // }
}
